import { Toast, Loading } from "../../utils/util";
Page({
  data: {
    form: {}
  },
  backLogin() {
    wx.navigateTo({
      url: '../denglu/denglu',
    })
  },
  onShow() {
    this.setData({
      form: {
        name: wx.getStorageSync('userInfo').name,
        sex: wx.getStorageSync('userInfo').sex,
      }
    })
  },
  checkSex() {
    this.setData({
      'form.sex': this.data.form.sex == "1" ? "2" : "1"
    })
  },
  formSubmit(e) {
    let formData = {
      ...this.data.form,
      ...e.detail.value
    };
    for (var i in formData) {
      if (formData[i] == '') {
        const msg = i == 'name' ? '用户名' : '其他';
        Toast("none", msg + "不能为空");
        return;
      }
    }
    wx.cloud.database().collection('user').doc(wx.getStorageSync('uid')).update({
      data: {
        ...formData
      },
      success(res) {
        Toast("none", "修改成功");
        let userInfo = wx.getStorageSync('userInfo')
        wx.setStorageSync('userInfo', {
          ...userInfo,
          ...formData
        })
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/setting/setting',
          })
        }, 1200)
      },
      fail(res) {
        Toast("none", "修改失败");
      },
    })
  },
})